package es.ats.codington.festival.junit;


import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.utils.FERSDataConnection;
import junit.framework.TestCase;

public class TestEventDAO extends TestCase {
	
	Event event;
	EventDAOImpl edao;
	Connection connection;
	int affectedRows;
	ArrayList<Event> eventList;
	int size;
	int size2;
	int id;
	
	@Before
	protected void setUp() throws Exception {
		super.setUp();
		connection = FERSDataConnection.getConnection();
		connection.setAutoCommit(false);
		edao = new EventDAOImpl(connection);
		event = new Event("match1","futbol",3,"sport",1000,1000,"stadium");
		eventList = new ArrayList<Event>();	
		size=3;
		size2=2;
		id=1;
	}
	
	@After
	protected void tearDown() throws Exception {
		super.tearDown();
		connection.rollback();
		connection.close();
		
	}
	
	
	@Test
	public void testEventDAORegisterNewEvent() {
		
		try {
			affectedRows=1;
			assertEquals(edao.registerNewEvent(event),affectedRows);
	
		} catch (Exception ex) {
			fail("No exception should be thrown");
		}	
		
	}
	
	@Test
	public void testEventDAOShowEventsByPlace() {
	
		try {
			Event event1=new Event();
			event1.setPlace("Stadium");
			eventList=edao.showEventsByPlaceDAO(event1);
			assertEquals(eventList.size(),size2);
			
			/*
			for(int i=0;i<eventList.size();i++)
		    {
		 		Event event1 =eventList.get(i);
		    	System.out.println("Element["+i+"] is: "+event1.getEventName()+" "+event1.getDescription()+" "+event1.getDuration());
			
		    }
		*/
		} catch (Exception ex) {
			fail("No exception should be thrown");
		}	
	
	}	
	
		@Test
		public void testEventDAOShowEvents() {
		
			try {
				eventList=edao.showEvents();
				assertEquals(eventList.size(),size);
				
				/*
				for(int i=0;i<eventList.size();i++)
			    {
			 		Event event1 =eventList.get(i);
			    	System.out.println("Element["+i+"] is: "+event1.getEventName()+" "+event1.getDescription()+" "+event1.getDuration());
				
			    }
			*/
			} catch (Exception ex) {
				fail("No exception should be thrown");
			}	
		
		}	
		
		@Test
		public void testEventDAOGetIdEvent() {
			
			try {
				event.setEventName("Opera");
				assertEquals(edao.getIdEventDAO(event),2);
			} catch (Exception ex) {
				fail("No exception should be thrown");
			}	
			
		}
		
		@Test
		public void testEventDAOCheckEvent() {
			
			try {
				event.setEventName("Opera");
				assertTrue(edao.checkEventDAO(event));
		
			} catch (Exception ex) {
				fail("No exception should be thrown");
			}	
			
		}
	
		
		@Test
		public void testEventDAOUpdateEvent() {
			
			try {
				affectedRows=1;
				event.setEventName("Opera");
				event.setIdEvent(edao.getIdEventDAO(event));
				assertEquals(edao.updateEvent(event),affectedRows);
		
			} catch (Exception ex) {
				fail("No exception should be thrown");
			}
			
		}
		
		@Test
		public void testEventDAODeleteEvent() {
			
			try {
				affectedRows=1;
				event.setEventName("Opera");
				event.setIdEvent(edao.getIdEventDAO(event));
				assertEquals(edao.deleteEvent(event.getIdEvent()),affectedRows);
		
			} catch (Exception ex) {
				fail("No exception should be thrown");
			}	
			
		}
		
		/*
		@Test
		public void testEventDAOShowEventsByIdPlace() {
		
			try {
				
				event.setIdPlace(edao.getIdPlaceDAO(place));
				eventList=edao.showEventsByIdPlaceDAO(event);
				assertEquals(eventList.size(),size);
				/*
				for(int i=0;i<eventList.size();i++)
			    {
			 		Event event1 =eventList.get(i);
			    	System.out.println("Element["+i+"] is: "+event1.getEventName()+" "+event1.getDescription()+" "+event1.getDuration());
				assertEquals(es.registerNewEvent(event),affectedRows);
			    }
			} catch (Exception ex) {
				fail("No exception should be thrown");
			}	
		
		}	
		*/
		
	}

